{
  "nbformat": 4,
  "nbformat_minor": 5,
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3.10.0"
    },
    "dataset_doi": "https://doi.org/10.7910/DVN/AHUZZ0",
    "author": "de la Serna, Juan Moises",
    "date": "2026-03-02"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Analisis Reproducible\n",
        "## MBI en America Latina - Base de Datos Estadisticos\n",
        "**DOI**: https://doi.org/10.7910/DVN/AHUZZ0\n",
        "**Autor**: de la Serna, Juan Moises | **Fecha**: 2026-03-02"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 0. Instalacion de Dependencias\n",
        "Ejecutar solo si no tiene instaladas las librerias:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# !pip install pandas numpy scipy matplotlib seaborn pyDataverse requests\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom scipy import stats\nimport warnings\nwarnings.filterwarnings('ignore')\nprint('Librerias cargadas exitosamente')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 1. Carga de Datos desde Harvard Dataverse"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import requests\n\n# DOI del dataset\nDATASET_DOI = 'doi:10.7910/DVN/AHUZZ0'\nSERVER = 'https://dataverse.harvard.edu'\n\n# Obtener lista de archivos del dataset\nurl = f'{SERVER}/api/datasets/:persistentId/?persistentId={DATASET_DOI}'\nresponse = requests.get(url)\ndata = response.json()\nfiles = data['data']['latestVersion']['files']\nprint(f'Archivos encontrados: {len(files)}')\nfor f in files:\n    print(f\"  - {f['label']} ({f['dataFile']['contentType']})\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Descargar archivo de estudios clinicos\nfor f in files:\n    if 'Estudios_Clinicos' in f['label']:\n        file_id = f['dataFile']['id']\n        break\n\nurl_data = f'{SERVER}/api/access/datafile/{file_id}'\ndf_clinicos = pd.read_csv(url_data, sep='\\t')\nprint('Dataset de estudios clinicos cargado:')\nprint(df_clinicos.shape)\ndf_clinicos.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Descargar archivo de prevalencia\nfor f in files:\n    if 'Prevalencia' in f['label']:\n        file_id_prev = f['dataFile']['id']\n        break\n\nurl_prev = f'{SERVER}/api/access/datafile/{file_id_prev}'\ndf_prevalencia = pd.read_csv(url_prev, sep='\\t')\nprint('Dataset de prevalencia cargado:')\ndf_prevalencia.head()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 2. Estadisticas Descriptivas"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print('=== ESTADISTICAS DESCRIPTIVAS - ESTUDIOS CLINICOS ===')\ndf_clinicos.describe().round(3)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print('=== PREVALENCIA POR PAIS ===')\ndf_prevalencia.describe().round(3)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 3. Visualizaciones"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "fig, axes = plt.subplots(2, 2, figsize=(14, 10))\nfig.suptitle('MBI en America Latina: Analisis Estadistico', fontsize=14, fontweight='bold')\n\n# Grafico 1: Prevalencia por pais\nif 'pais' in df_prevalencia.columns and 'prevalencia_pct' in df_prevalencia.columns:\n    axes[0,0].barh(df_prevalencia['pais'], df_prevalencia['prevalencia_pct'], color='steelblue')\n    axes[0,0].axvline(x=4.7, color='red', linestyle='--', label='Promedio global (4.7%)')\n    axes[0,0].set_title('Prevalencia de Ansiedad por Pais (%)')\n    axes[0,0].legend()\n\n# Grafico 2: Reduccion de ansiedad pre-post\nif 'ansiedad_pre' in df_clinicos.columns and 'ansiedad_post' in df_clinicos.columns:\n    axes[0,1].scatter(df_clinicos['ansiedad_pre'], df_clinicos['ansiedad_post'], \n                      c='darkorange', s=100, alpha=0.7)\n    max_val = max(df_clinicos['ansiedad_pre'].max(), df_clinicos['ansiedad_post'].max())\n    axes[0,1].plot([0, max_val], [0, max_val], 'k--', label='Sin cambio')\n    axes[0,1].set_xlabel('Ansiedad Pre-intervencion')\n    axes[0,1].set_ylabel('Ansiedad Post-intervencion')\n    axes[0,1].set_title('Efecto MBI en Ansiedad (Pre vs Post)')\n    axes[0,1].legend()\n\nplt.tight_layout()\nplt.savefig('mbi_analisis_latinoamerica.png', dpi=150, bbox_inches='tight')\nplt.show()\nprint('Visualizacion guardada como mbi_analisis_latinoamerica.png')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 4. Pruebas Estadisticas"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Test de Wilcoxon (no parametrico) para diferencias pre-post\nif 'ansiedad_pre' in df_clinicos.columns and 'ansiedad_post' in df_clinicos.columns:\n    stat, p_value = stats.wilcoxon(df_clinicos['ansiedad_pre'], df_clinicos['ansiedad_post'])\n    print(f'Test de Wilcoxon (ansiedad pre vs post):')\n    print(f'  Estadistico W = {stat:.3f}')\n    print(f'  p-valor = {p_value:.4f}')\n    print(f'  Interpretacion: {\"Diferencia significativa\" if p_value < 0.05 else \"No significativo\"} (alpha=0.05)')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 5. Exportar Resultados"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Exportar estadisticas a CSV\nstats_summary = df_clinicos.describe()\nstats_summary.to_csv('estadisticas_descriptivas_mbi.csv')\nprint('Estadisticas exportadas a: estadisticas_descriptivas_mbi.csv')\n\nprint('\\n=== ANALISIS COMPLETADO ===')\nprint('Para reproducir, ejecute este notebook desde el inicio (Kernel > Restart & Run All)')\nprint('DOI del dataset: https://doi.org/10.7910/DVN/AHUZZ0')"
      ]
    }
  ]
}